%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function fun_segment_multi_edge( index, min_area, fudgeFactor)
global mov


I = mov(index).data;

% Detect Cell Edges
[~, threshold] = edge(I, 'sobel'); %sobel
BWs = edge(I,'sobel', threshold * fudgeFactor);

% Dilate the edges
se90 = strel('line', 3, 90); %originally set to 3
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);

% Fill
BWdfill = imfill(BWsdil, 'holes');

% Smooth Objects
seD = strel('diamond',1);  % originally set to 1
BWfinal = imerode(BWdfill,seD);
BWfinal = imerode(BWfinal,seD);

% Remove small area objects
temp = bwareaopen( BWfinal, min_area );

% SAVE IT HERE
mov(index).segmented = temp;
